package com.dengyuanke.upms.logic.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengyuanke.common.core.result.R;
import com.dengyuanke.common.log.annotation.SysLog;
import com.dengyuanke.common.security.annotation.Inner;
import com.dengyuanke.common.security.util.SecurityUtils;
import com.dengyuanke.upms.domain.dto.UserDTO;
import com.dengyuanke.upms.domain.dto.UserInfo;
import com.dengyuanke.upms.domain.entity.User;
import com.dengyuanke.upms.logic.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2019-06-29
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 获取当前用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping(value = {"/info"})
    public R info() {
        String username = SecurityUtils.getUsername();
        User user = userService.getOne(Wrappers.<User>query()
                .lambda().eq(User::getUsername, username));
        if (user == null) {
            return R.fail("获取当前用户信息失败");
        }
        UserInfo userInfo = userService.findUserInfo(user);
        userInfo.getUser().setPassword(null);
        return R.success(userInfo);
    }

    /**
     * 获取通过用户名获取用户全部信息
     *
     * @param username
     * @return 用户信息
     */
    @Inner
    @GetMapping("/info/{username}")
    public R<UserInfo> info(@PathVariable String username) {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (user == null) {
            return R.fail(String.format("用户信息为空 %s", username));
        }
        UserInfo userInfo = userService.findUserInfo(user);
        return R.success(userInfo);
    }

    /**
     * 通过ID查询用户信息
     *
     * @param id ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public R user(@PathVariable Long id) {
        return R.success(userService.getById(id));
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return
     */
    @GetMapping("/details/{username}")
    public R user(@PathVariable String username) {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        return R.success(user);
    }

    /**
     * 删除用户信息
     *
     * @param id ID
     * @return R
     */
    @DeleteMapping("/{id}")
    public R userDel(@PathVariable Long id) {
        return R.success(userService.removeById(id));
    }

    /**
     * 添加用户
     *
     * @param user 用户信息
     * @return success/false
     */
    @PostMapping
    @SysLog("添加用户")
    @PreAuthorize("hasAuthority('sys_user_add')")
    public R user(@RequestBody User user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        return R.success(userService.save(user));
    }


    @GetMapping("/page")
    public R UsersPage(Page page,User user) {
        String username = user.getUsername();
        LambdaQueryWrapper<User> objectLambdaQueryWrapper = new LambdaQueryWrapper<User>();
        if(StrUtil.isNotBlank(username)) {
            objectLambdaQueryWrapper.like(User::getUsername,username);
        }
        return R.success(userService.page(page,objectLambdaQueryWrapper));
    }
}

